Alter Procedure sp_ViewEmployeByCourse
(
	@TRID int, 
	@MainID int, 
	@SubID int
)
As 
--Declare @TRID int, @MainID int, @SubID int
--Select @TRID = 2
--Select @MainID = 2
--Select @TRID = 8

IF @TRID > 0 And @MainID > 0 And @SubID > 0
	Begin
		SELECT     TA.TR_ICNO, TA.TR_EmpName, TA.TR_DateJoin
		FROM         tblTrainingCourse AS TC INNER JOIN
						  tblTrainingAttendance AS TA ON TC.TR_ID = TA.TR_ID
		WHERE     (TC.TR_ID = @TRID) AND (TC.TR_MainCatID = @MainID) AND (TC.TR_SubCatID = @SubID)
	END
ELSE IF @TRID = -1 And @MainID > 0 And @SubID > 0
	Begin
		SELECT     TA.TR_ICNO, TA.TR_EmpName, TA.TR_DateJoin
		FROM         tblTrainingCourse AS TC INNER JOIN
						  tblTrainingAttendance AS TA ON TC.TR_ID = TA.TR_ID
		WHERE     (TC.TR_MainCatID = @MainID) AND (TC.TR_SubCatID = @SubID)
	END

ELSE IF @TRID = -1 And @MainID > 0 And @SubID = -1
	Begin
		SELECT     TA.TR_ICNO, TA.TR_EmpName, TA.TR_DateJoin
		FROM         tblTrainingCourse AS TC INNER JOIN
						  tblTrainingAttendance AS TA ON TC.TR_ID = TA.TR_ID
		WHERE     (TC.TR_MainCatID = @MainID)
	END

--Exec sp_ViewEmployeByCourse -1,2,-1